﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Reflection;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Collections.Specialized;
using System.Collections;
using System.Xml.Serialization;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace GeneralUtilities
{
    public static class utilities
    {
        public static void CopyProperties(object objFrom, object objTo, string[] ExcludeProperties)
        {
            foreach (System.Reflection.PropertyInfo oProperty in objFrom.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if ((ExcludeProperties == null) || !ExcludeProperties.Contains(oProperty.Name))
                {
                    System.Reflection.PropertyInfo oNewProperty = objTo.GetType().GetProperty(oProperty.Name);
                    if (oNewProperty != null && oNewProperty.CanWrite)
                    {
                        oNewProperty.SetValue(objTo, oProperty.GetValue(objFrom, null), null);
                    }
                }
            }
        }

		//public static string GetConnectionString(string connectionName)
		//{
		//	return System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString();
		//}

    }

    public static class ConfigUtilities
    {
        public static string GetCEDbConnElement(string connectionStringName, string elementName)
        {
            string elementValue = string.Empty;
            string itemUpper;

            elementName = elementName.ToUpper().Trim() + "=";
            // Get the server name from the connection string
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            string[] split = connStr.Split(new Char[] { ';' });

            foreach (string item in split)
            {
                itemUpper = item.ToUpper().Trim();
                if (itemUpper.Contains(elementName))
                {
                    elementValue = itemUpper.Substring(itemUpper.IndexOf(elementName)+elementName.Length);
                    elementValue = elementValue.Replace("\"",string.Empty);
                    elementValue = elementValue.Replace("'", string.Empty);
                    break;
                }
            }

            return elementValue;

        }

    }

    public static class IOUtilities
    {
        #region File I/O

        public static string Compress(string filename)
        {
            FileInfo fi = new FileInfo(filename);
            string compressedFileName = string.Empty;

            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Prevent compressing hidden and already compressed files.
                if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
                        != FileAttributes.Hidden & fi.Extension != ".gz")
                {
                    compressedFileName = fi.FullName.Replace(".bak", string.Empty) + ".gz";
                    // Create the compressed file.
                    using (FileStream outFile = File.Create(compressedFileName))
                    {
                        using (GZipStream Compress = new GZipStream(outFile,
                                CompressionMode.Compress))
                        {
                            // Copy the source file into the compression stream.
                            byte[] buffer = new byte[4096];
                            int numRead;
                            while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                Compress.Write(buffer, 0, numRead);
                            }
                        }
                    }
                }
            }

            return compressedFileName;
        }

        public static bool Decompress(string filename, string targetFileName)
        {
            bool success = false;
            try
            {
                FileInfo fi = new FileInfo(filename);
                // Get the stream of the source file.
                using (FileStream inFile = fi.OpenRead())
                {
                    // Get original file extension, for example "doc" from report.doc.gz.
                    string curFile = fi.FullName;

                    //Create the decompressed file.
                    using (FileStream outFile = File.Create(targetFileName))
                    {
                        using (GZipStream Decompress = new GZipStream(inFile,
                                CompressionMode.Decompress))
                        {
                            //Copy the decompression stream into the output file.
                            byte[] buffer = new byte[4096];
                            int numRead;
                            while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                outFile.Write(buffer, 0, numRead);
                            }
                        }
                    }

                    success = true;
                }
            }
            catch
            {
                success = false;
            }

            return success;
        }

        public static void LogitToDisk(string filename, string message, bool RecrearSinTimestamp)
        {
            if (RecrearSinTimestamp)
            { File.Delete(filename); }
            try
            {
                StreamWriter tw;

                if (!File.Exists(filename))
                { tw = File.CreateText(filename); }
                else
                { tw = new StreamWriter(filename, true); }

                tw.WriteLine(message);
                tw.Flush();
                tw.Close();

            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("GanagroLite_LogitToDisk", ex.Message);
            }

        }

        public static void LogitToDisk(string filename, string message)
        {
            try
            {

                StreamWriter tw;

                if (!File.Exists(filename))
                { tw = File.CreateText(filename); }
                else
                { tw = new StreamWriter(filename, true); }

                message = DateTime.Now.ToString() + " " + message;
                tw.WriteLine(message);
                tw.Flush();
                tw.Close();

            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("GanagroLite_LogitToDisk", ex.Message);
            }
        }

        #endregion

    }

    public static class BackupUtilities
    {
        #region Database Backup / restore

        public static DateTime BdUploaded { get; set; }

        public static DateTime FechaUltimaActualizacion { get; set; }

        public static void CopiarADirNubeLocal(string dbfile, string nombreArchivoCopia, string directorioLocalNube)
        {
            string ArchivolocalNube = System.IO.Path.Combine(directorioLocalNube, nombreArchivoCopia);

            if (ArchivolocalNube.Trim() != string.Empty)
            {
                System.IO.File.Copy(dbfile, @ArchivolocalNube, true);
            }
        }

       #endregion

    }

}

